from skimage import data, color,io
from matplotlib import pyplot as plt
import numpy as np
#定义灰度级到彩色变换    
L = 255    
def GetR(gray):
    if gray < L / 2:
        return 0
    elif gray > L / 4 * 3:
        return L
    else:
        return 4 * gray - 2 * L
    
def GetG(gray):
    if gray < L / 4:
        return 4 * gray
    elif gray > L / 4 * 3:
        return 4 * L - 4 * gray
    else:
        return L
        
def GetB(gray):
    if gray < L / 4:
        return L
    elif gray > L / 2:
        return 0
    else:
        return 2 * L - 4 * gray

# img = data.coffee()
# image = io.imread(r'C:\Users\26356\Desktop\python\lenagray.jpg')
image = io.imread(r'C:\Users\26356\Desktop\python\test.jpg')
grayimg = color.rgb2gray(image) * 255 #将彩色图像转化为灰度图像
colorimg = np.zeros(image.shape,dtype='uint8')
for ii in range(image.shape[0]):
    for jj in range(image.shape[1]):
        r,g,b = GetR(grayimg[ii, jj]), GetG(grayimg[ii, jj]), GetB(grayimg[ii, jj])
        colorimg[ii,jj,:]=(r, g, b)

plt.rcParams['font.sans-serif'] = ['SimHei'] 
plt.rcParams['axes.unicode_minus'] = False    
plt.subplot(1,2,1)
plt.axis('off') 
plt.imshow(grayimg, cmap='gray')
plt.title('灰度图像')
plt.subplot(1,2,2)
plt.axis('off') 
plt.imshow(colorimg)
plt.title('伪彩色图像')
plt.show()
# plt.savefig('Intensity2Color.tif')


# from skimage import data, color, io
# from matplotlib import pyplot as plt
# import numpy as np

# # 定义灰度级到彩色变换    
# L = 255    

# def GetR(gray):
#     if gray < L / 2:
#         return 0
#     elif gray > L / 4 * 3:
#         return L
#     else:
#         return 4 * gray - 2 * L

# def GetG(gray):
#     if gray < L / 4:
#         return 4 * gray
#     elif gray > L / 4 * 3:
#         return 4 * L - 4 * gray
#     else:
#         return L

# def GetB(gray):
#     if gray < L / 4:
#         return L
#     elif gray > L / 2:
#         return 0
#     else:
#         return 2 * L - 4 * gray

# # 读取灰度图像
# # image = io.imread(r'C:\Users\26356\Desktop\python\lenagray.jpg')
# image = io.imread(r'C:\Users\26356\Desktop\python\test.jpg')

# grayimg = color.rgb2gray(image) * 255  # 将彩色图像转化为灰度图像

# # 生成伪彩色图像
# colorimg = np.zeros(image.shape, dtype='uint8')
# for ii in range(image.shape[0]):
#     for jj in range(image.shape[1]):
#         r, g, b = GetR(grayimg[ii, jj]), GetG(grayimg[ii, jj]), GetB(grayimg[ii, jj])
#         colorimg[ii, jj, :] = (r, g, b)

# # 显示原始RGB图像
# plt.rcParams['font.sans-serif'] = ['SimHei']
# plt.rcParams['axes.unicode_minus'] = False

# plt.subplot(2, 3, 1)
# plt.axis('off')
# plt.imshow(image)
# plt.title('原始RGB图像')

# # 显示R、G、B三个分量的图像
# plt.subplot(2, 3, 2)
# plt.axis('off')
# plt.imshow(image[:, :, 0], cmap='gray')
# plt.title('R分量图像')

# plt.subplot(2, 3, 3)
# plt.axis('off')
# plt.imshow(image[:, :, 1], cmap='gray')
# plt.title('G分量图像')

# plt.subplot(2, 3, 4)
# plt.axis('off')
# plt.imshow(image[:, :, 2], cmap='gray')
# plt.title('B分量图像')

# # 显示红色分割后的图像
# plt.subplot(2, 3, 5)
# plt.axis('off')
# plt.imshow(colorimg[:, :, 0], cmap='gray')
# plt.title('红色分割图像')
# # 显示分割后的RGB图像
# plt.subplot(2, 3, 6)
# plt.axis('off')
# plt.imshow(colorimg)
# plt.title('分割后的RGB图像')

# plt.show()
